<?php
namespace Dashboard\Controller;
use Think\Upload;

class IconsController extends BaseController
{
    public function index()
    {
        $a = (int)(I("currpage"));//跳转页数
        $sou = I('sou');
        $pagesize = 50;
        if ($a != null) {
            $currpage = $a;
        } else {
            $currpage = isset($_GET['currpage']) ? $_GET['currpage'] : 1;
        }
        if ($sou) {
            $content = I('content');
            $totalpage = 1;
            $currpage = 1;
            if ($sou == 1) {
                $list = sels("homes", "", "name like '%{$content}%'", "id desc");
            }
        } else {
            $result = currpage("icons", "", $pagesize, $currpage, "id asc");
            $totalpage = $result['totalpage'];
            $list = $result['list'];
            if ($result['currpage']) {
                $currpage = $result['currpage'];
            }
        }
        $this->assign("list", $list)->assign("currpage", $currpage)->assign("totalpage", $totalpage);
        $this->display();
    }

    public function form()
    {
        $id = I('id');
        if ($id) {
            $list = sel("icons", " * ", "id = {$id}");
            $this->assign("list", $list);
        }
        $this->display();
    }

//添加分类
    public function checks()
    {
        $id = I('id');
        $name = I("name");
        $url = I("urls");
        if ($id == 0) {
            //处理图片
            if ($_FILES['one']['name'] != null) {
                $info = $this->imgUpload("icons/");
                $savename = $info['one']['savename'];
                $savepath = $info['one']['savepath'];
                $data['img'] = "Data/" . $savepath . $savename;
            }
            $res = ins("icons", array("name" => $name, "create_at" => time(), "img" => $data['img'], "url" => $url));
            if ($res) {
                $this->success("添加成功", U("form"));
            }
        } else {
            $tt = sel("icons", " * ", array("id" => $id));

            $data['img'] = I('img');
            //处理图片
            if ($_FILES['one']['name'] != null) {
                unlink($data['img']);
                $info = $this->imgUpload("icons/");
                $savename = $info['one']['savename'];
                $savepath = $info['one']['savepath'];
                $data['img'] = "Data/" . $savepath . $savename;
            }
            $res = up_one("icons", array("id" => $id), array("name" => $name, "up_at" => time(), "img" => $data['img'], "url" => $url));
            if ($res) {
                $this->success("修改成功", U("index"));
            }
        }
    }
}